package com.simas.screen;

import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
//import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
//import net.rim.device.api.ui.Graphics;
//import net.rim.device.api.ui.component.BitmapField;
//import net.rim.device.api.ui.component.Dialog;
//import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.picker.DateTimePicker;
import net.rim.device.api.ui.FieldChangeListener;

//import com.simas.component.CellButton;
import com.simas.component.CustomButton;
import com.simas.common.CommonFunction;
import com.simas.common.CommonMenu;
import com.simas.common.CustomPersistentObject;
import com.simas.common.CustomResources;
import com.simas.hid.CallHIDServlet;
import com.simas.mobil.CallMBUServlet;
//import com.simas.common.DeletePolisInfo;
//import com.simas.common.OpenInfoScreen;
//import com.simas.common.OpenPolisScreen;
//import com.simas.common.hid.CallHIDServlet;
//import com.simas.common.mbu.CallMBUServlet;
//import com.simas.component.GridFieldManager;

public class LoginScreen extends MainScreen implements FieldChangeListener {
	private CustomButton btHome;
	
	CustomResources cr = new CustomResources();
	CommonFunction cf = new CommonFunction();
	
	private LabelField lblNama = new LabelField("Nama : ", Field.FIELD_RIGHT);
	private EditField nama = new EditField("", "");
	
	private LabelField lblNoKartu = new LabelField("No Kartu : ", Field.FIELD_RIGHT);
	private EditField noKartu = new EditField("", "");
	
	private LabelField lblTglLahir = new LabelField("Tgl Lahir : ", Field.FIELD_RIGHT);
	private LabelField lblTglLahir1 = new LabelField(cf.getTodayDate("dd/MM/yyyy"), Field.FIELD_LEFT);
	
	private LabelField lblNoPlat = new LabelField("No Plat : ", Field.FIELD_RIGHT);
	private EditField noPlat = new EditField("", "");
	
	private LabelField lblNoPolis = new LabelField("No Polis : ", Field.FIELD_RIGHT);
	private EditField noPolis = new EditField("", "");
	
	private LabelField lblNoPeserta = new LabelField("No Peserta : ", Field.FIELD_RIGHT);
	private EditField noPeserta = new EditField("", "");
	
	private LabelField lblNoRangka = new LabelField("No Rangka : ", Field.FIELD_RIGHT);
	private EditField noRangka = new EditField("", "");
	
	private LabelField lblNoMesin = new LabelField("No Mesin : ", Field.FIELD_RIGHT);
	private EditField noMesin = new EditField("", "");
	
	private CustomButton btClear;
	private CustomButton btSubmit;
	private CustomButton btDate;
	
	private int intIdxPolis;
	
	/**
     * Menambah Menu Close
     */
    protected void makeMenu( Menu menu, int instance ) {
		CommonMenu cm = new CommonMenu(this);
		menu.add(cm._welcomeScreen);
	}
	   
    /**
     * Action utk Back Button
     */
    protected boolean keyDown(int keycode, int status)  {
		if(Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
			CommonMenu cm = new CommonMenu(this);
			cm.welcomeScreen();
			return true;
		}
		return false;
	}
	
    /**
     * Action Listener
     */
	public void fieldChanged(Field field, int context) {
		if (field == btHome) {
			CommonMenu cm = new CommonMenu(this);
			cm.welcomeScreen();
		} else if (field == btClear) {
			clearTextField();
		} else if (field == btSubmit) {
			CustomPersistentObject cpo = new CustomPersistentObject();
			Hashtable htData = new Hashtable();
			if (intIdxPolis == 1) {
				// simas sehat
				//fnOpenHidScreen(htPersistentData);
				htData.put("NAMA", nama.getText());
				htData.put("NOKARTU", noKartu.getText());
				htData.put("NOPOLIS", noPolis.getText());
				htData.put("NOPESERTA", noPeserta.getText());
				htData.put("TGLLAHIR", lblTglLahir1.getText());
				cpo.SavePersistentObject(htData,intIdxPolis);
				fnOpenHidScreen();
			} else if (intIdxPolis == 2) {
				// simas mobil
				htData.put("NOPLAT", noPlat.getText());
				htData.put("NOPOLIS", noPolis.getText());
				htData.put("NOMESIN", noMesin.getText());
				htData.put("NORANGKA", noRangka.getText());
				cpo.SavePersistentObject(htData,intIdxPolis);
				fnOpenMbuScreen();
			}
		} else if (field == btDate) {
			fnDatePicker();
		}
	}
	
	private void fnDatePicker() {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				DateTimePicker datePicker = DateTimePicker.createInstance(Calendar.getInstance(),"dd MMM yyyy",null);
				datePicker.doModal();
				Calendar cal = datePicker.getDateTime();
				Date date = cal.getTime();
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				//Dialog.alert("You selected " + date.toString());
				lblTglLahir1.setText(sdf.format(date));
			}
		});
	}
	
	private void clearTextField() {
		nama.setText("");
		noKartu.setText("");
		lblTglLahir1.setText(cf.getTodayDate("dd/MM/yyyy"));
		noPlat.setText("");
		noPolis.setText("");
		noPeserta.setText("");
		noRangka.setText("");
		noMesin.setText("");
	}
	
	private void fnOpenHidScreen() {
		CallHIDServlet chs = new CallHIDServlet();
		chs.fnCallHIDServlet(this, noKartu.getText(), noPolis.getText(), noPeserta.getText(), lblTglLahir1.getText(), nama.getText());
	}
	
	private void fnOpenMbuScreen() {
		CallMBUServlet cms = new CallMBUServlet();
		cms.fnCallMBUServlet(this, noPlat.getText(), noPolis.getText(), noRangka.getText(), noMesin.getText());
	}
	
	public LoginScreen(int intIndex) {
			intIdxPolis = intIndex;
			
			Bitmap resizeBitmap = null;
			if (intIndex == 1) {
				resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("bgSimasSehat.png"));
			} else if (intIndex == 2) {
				resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("bgSimasMobil.png"));
			}
			Background bg = BackgroundFactory.createBitmapBackground(resizeBitmap, 
																	 Background.POSITION_X_LEFT, 
																	 Background.POSITION_Y_TOP, 
																	 Background.REPEAT_NONE);
			VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL);
			vfm.setBackground(bg);
			vfm.setFont(cr.getCustomFont(4));
			for (int a=1; a<=7; a++) {
				vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
			}
			
			// btHome
//			btHome = new CustomButton("Home", Color.WHITE, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
//			btHome.setChangeListener(this);
//			
//			HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.FIELD_LEFT);
//			hfmButton.add(btHome);
//			vfm.add(hfmButton);
//			
//			vfm.add(new LabelField(" ", Field.USE_ALL_WIDTH));
						
			// form login
			if (intIndex != 0) {
				if (intIndex == 1) {
					// simas sehat
			
					VerticalFieldManager hfm = new VerticalFieldManager(Field.FIELD_LEFT);
					hfm.setFont(cr.getCustomFont());
					LabelField infoField1 = new LabelField("Isikan data peserta yang anda miliki, dengan ketentuan: ", Field.FIELD_LEFT);
					LabelField infoField2 = new LabelField("1. Kombinasi Nama + Tgl Lahir", Field.FIELD_LEFT);
					LabelField infoField3 = new LabelField("2. Kombinasi No Kartu + Tgl Lahir", Field.FIELD_LEFT);
					LabelField infoField4 = new LabelField("3. Kombinasi No Polis + No Peserta + Tgl Lahir", Field.FIELD_LEFT);
					LabelField infoField5 = new LabelField("pada form di bawah ini", Field.FIELD_LEFT);
					LabelField infoField6 = new LabelField(" ", Field.FIELD_LEFT);
					hfm.add(infoField1);
					hfm.add(infoField2);
					hfm.add(infoField3);
					hfm.add(infoField4);
					hfm.add(infoField5);
					hfm.add(infoField6);
					vfm.add(hfm);
					
					VerticalFieldManager inputManager = new VerticalFieldManager();
					inputManager.setFont(cr.getCustomFont());
					inputManager.add(lblNama);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblNoKartu);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblNoPolis);
					inputManager.add(lblNoPeserta);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblTglLahir);
					
					VerticalFieldManager inputManager1 = new VerticalFieldManager();
					inputManager1.setFont(cr.getCustomFont());
					nama.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noKartu.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noPolis.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noPeserta.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					inputManager1.add(nama);
					inputManager1.add(new LabelField("Atau", Field.FIELD_LEFT));
					inputManager1.add(noKartu);
					inputManager1.add(new LabelField("Atau", Field.FIELD_LEFT));
					inputManager1.add(noPolis);
					inputManager1.add(noPeserta);
					inputManager1.add(new LabelField("Dan", Field.FIELD_LEFT));
					
					btDate = new CustomButton("Date", Color.WHITE, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
					btDate.setChangeListener(this);
					HorizontalFieldManager dateManager = new HorizontalFieldManager(Field.FIELD_LEFT);
					dateManager.setFont(cr.getCustomFont());
					dateManager.add(lblTglLahir1);
					dateManager.add(btDate);
					inputManager1.add(dateManager);
					
					HorizontalFieldManager loginManager = new HorizontalFieldManager(Field.FIELD_LEFT);
					loginManager.setFont(cr.getCustomFont());
					loginManager.add(inputManager);
					loginManager.add(inputManager1);
					vfm.add(loginManager);
					
					//noKartu.setFocus();
					
				} else if (intIndex == 2) {
					// simas mobil
					
					VerticalFieldManager hfm = new VerticalFieldManager(Field.FIELD_LEFT);
					hfm.setFont(cr.getCustomFont());
					LabelField infoField1 = new LabelField("Isikan data peserta yang anda miliki, pilih salah satu: ", Field.FIELD_LEFT);
					LabelField infoField2 = new LabelField("1. No Polis", Field.FIELD_LEFT);
					LabelField infoField3 = new LabelField("2. No Plat Kendaraan", Field.FIELD_LEFT);
					LabelField infoField4 = new LabelField("3. No Rangka Kendaraan", Field.FIELD_LEFT);
					LabelField infoField5 = new LabelField("4. No Mesin Kendaraan", Field.FIELD_LEFT);
					LabelField infoField6 = new LabelField("pada form di bawah ini", Field.FIELD_LEFT);
					LabelField infoField7 = new LabelField(" ", Field.FIELD_LEFT);
					hfm.add(infoField1);
					hfm.add(infoField2);
					hfm.add(infoField3);
					hfm.add(infoField4);
					hfm.add(infoField5);
					hfm.add(infoField6);
					hfm.add(infoField7);
					vfm.add(hfm);
					
					VerticalFieldManager inputManager = new VerticalFieldManager();
					inputManager.setFont(cr.getCustomFont());
					inputManager.add(lblNoPolis);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblNoPlat);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblNoRangka);
					inputManager.add(new LabelField(" ", Field.FIELD_LEFT));
					inputManager.add(lblNoMesin);
					
					VerticalFieldManager inputManager1 = new VerticalFieldManager();
					inputManager1.setFont(cr.getCustomFont());
					noPlat.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noRangka.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noMesin.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					noPolis.setBackground(BackgroundFactory.createSolidBackground(Color.KHAKI));
					inputManager1.add(noPolis);
					inputManager1.add(new LabelField("Atau", Field.FIELD_LEFT));
					inputManager1.add(noPlat);
					inputManager1.add(new LabelField("Atau", Field.FIELD_LEFT));
					inputManager1.add(noRangka);
					inputManager1.add(new LabelField("Atau", Field.FIELD_LEFT));
					inputManager1.add(noMesin);
					
					HorizontalFieldManager loginManager = new HorizontalFieldManager(Field.FIELD_LEFT);
					loginManager.setFont(cr.getCustomFont());
					loginManager.add(inputManager);
					loginManager.add(inputManager1);
					vfm.add(loginManager);
					
					//noPlat.setFocus();
					
				}
			}
			
			// menu bawah
			btClear = new CustomButton("Cancel", Color.WHITE, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
	        btClear.setChangeListener(this);
			btSubmit = new CustomButton("Submit", Color.WHITE, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
			btSubmit.setChangeListener(this);
			btHome = new CustomButton("Home", Color.WHITE, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
			btHome.setChangeListener(this);
			
			HorizontalFieldManager buttonManager1 = new HorizontalFieldManager(Field.FIELD_RIGHT);
			buttonManager1.setFont(cr.getCustomFont());
			buttonManager1.add(btSubmit);
			buttonManager1.add(btClear);
			buttonManager1.add(btHome);
			vfm.add(buttonManager1);
			
			add(vfm);
	}
}
